/*
 * 文件名：UserController.java
 * 版权： Copyright 2002-2007 12316yun. All Rights Reserved.
 * 描述：〈描述〉
 
 * 修改人：王钰玺
 * 修改时间：2018/4/26 16:02
 * 修改单号：〈修改单号〉
 * 修改内容：〈修改内容〉
 */
package com.yun12316.project.system.demopart.controller;

import com.github.pagehelper.PageInfo;
import com.yun12316.framework.web.controller.BaseController;
import com.yun12316.project.system.demopart.domain.User;
import com.yun12316.project.system.demopart.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * simple introduction
 * <p>
 * <p>detailed comment </p>
 *
 * @author 王钰玺 2018/4/26
 * @see
 * @since 1.0
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController extends BaseController {


    @Resource
    private IUserService userService;

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public PageInfo<User> getAll() {
        List<User> UserList = userService.selectAll();
        return new PageInfo<User>(UserList);
    }

    @RequestMapping(value = "/view/{id}")
    public User view(@PathVariable Integer id) {
        ModelAndView result = new ModelAndView();
        User User = userService.selectByPrimaryKey(id);
        return User;
    }

    @RequestMapping(value = "/delete/{id}")
    public ModelMap delete(@PathVariable Integer id) {
        ModelMap result = new ModelMap();
        userService.deleteByPrimaryKey(id);
        result.put("msg", "删除成功!");
        return result;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelMap save(User User) {
        ModelMap result = new ModelMap();
        String msg = User.getId() == null ? "新增成功!" : "更新成功!";
        userService.save(User);
        result.put("User", User);
        result.put("msg", msg);
        return result;
    }

}
